package com.acxiom.bsh.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acxiom.bsh.request.dto.RefundDTO;
import com.acxiom.bsh.request.dto.RefundsRecevieDTO;
import com.acxiom.bsh.request.dto.TradeFullDTO;
import com.acxiom.bsh.request.dto.TradeSoldDTO;
import com.acxiom.bsh.response.RefundResult;
import com.acxiom.bsh.response.RefundsRecevieResult;
import com.acxiom.bsh.response.TradeFullResult;
import com.acxiom.bsh.response.TradeSoldResult;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.RefundGetRequest;
import com.taobao.api.request.RefundsReceiveGetRequest;
import com.taobao.api.request.TradeFullinfoGetRequest;
import com.taobao.api.request.TradesSoldGetRequest;
import com.taobao.api.response.RefundGetResponse;
import com.taobao.api.response.RefundsReceiveGetResponse;
import com.taobao.api.response.TradeFullinfoGetResponse;
import com.taobao.api.response.TradesSoldGetResponse;

/**
 * @Description: TODO
 * @author jerche
 *
 * @updateUser
 * @updateDate
 */
@Controller
@RequestMapping("")
public class TradesController {

	@Value("${tmall.serverUrl}")
	private String serverurl;
	@Value("${tmall.appKey}")
	private String appKey;
	@Value("${tmall.appSecret}")
	private String appSecret;
	@Value("${tmall.sessionKeygf}")
	private String sessionKeygf;
	@Value("${tmall.sessionKeycw}")
	private String sessionKeycw;
	@Value("${tmall.sessionKeysh}")
	private String sessionKeysh;

	@RequestMapping(value = "/trades/sold/get")
	@ResponseBody
	public TradeSoldResult getTrades(@RequestBody TradeSoldDTO tradeDTO) {
		TradeSoldResult result = new TradeSoldResult();
		try {
			StringBuffer fieldStr = new StringBuffer();
			for (int i = 0; i < tradeDTO.getFields().length; i++) {
				fieldStr.append(tradeDTO.getFields()[i]);
				if (i < tradeDTO.getFields().length - 1)
					fieldStr.append(",");
			}
			TaobaoClient client = new DefaultTaobaoClient(serverurl, appKey, appSecret);
			TradesSoldGetRequest req = new TradesSoldGetRequest();
			req.setFields(fieldStr.toString());
			req.setStartCreated(com.taobao.api.internal.util.StringUtils.parseDateTime(tradeDTO.getStart_created()));
			req.setEndCreated(com.taobao.api.internal.util.StringUtils.parseDateTime(tradeDTO.getEnd_created()));

			if (tradeDTO.getPage_no() != null) {
				req.setPageNo(tradeDTO.getPage_no());
			}
			if (tradeDTO.getPage_size() != null) {
				req.setPageSize(tradeDTO.getPage_size());
			}
			if (tradeDTO.getUse_has_next() != null) {
				req.setUseHasNext(tradeDTO.getUse_has_next());
			}
			if (tradeDTO.getStatus() != null) {
				req.setStatus(tradeDTO.getStatus());
			}
			if (tradeDTO.getType() != null) {
				req.setType(tradeDTO.getType());
			}
			if (tradeDTO.getExt_type() != null) {
				req.setExtType(tradeDTO.getExt_type());
			}
			if (tradeDTO.getBuyer_nick() != null) {
				req.setBuyerNick(tradeDTO.getBuyer_nick());
			}
			if (tradeDTO.getTag() != null) {
				req.setTag(tradeDTO.getTag());
			}

			TradesSoldGetResponse rsp;
			String sessionKey = "";
			if (tradeDTO.getSeller_name().equals("西门子家电官方旗舰店")) {
				sessionKey = sessionKeygf;
			} else if (tradeDTO.getSeller_name().equals("西门子家电厨卫旗舰店")) {
				sessionKey = sessionKeycw;
			} else if (tradeDTO.getSeller_name().equals("博西华售后专营店")) {
				sessionKey = sessionKeysh;
			}

			rsp = client.execute(req, sessionKey);
			if (!rsp.isSuccess()) {
				JSONObject json = JSONObject.parseObject(rsp.getBody());
				String sub_code = JSONObject.parseObject(json.getString("error_response")).getString("sub_code");
				result.setError_code(sub_code);
				result.setSuccess(false);
				return result;
			}

			if (result == null) {
				result = new TradeSoldResult();
			}
			String tradesjson = JSONObject.parseObject(rsp.getBody()).getString("trades_sold_get_response");
			result = JSONObject.parseObject(tradesjson, TradeSoldResult.class);
			result.setSuccess(true);
			System.out.println("trades_sold_get_response :********" + rsp.getBody() + "******");
			System.out.println("响应结果:>>>>>" + JSONObject.toJSONString(result));

		} catch (ApiException e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/trade/full")
	@ResponseBody
	public TradeFullResult getTradeFull(@RequestBody TradeFullDTO tradeDTO) {
		TradeFullResult result = new TradeFullResult();
		try {
			StringBuffer fieldStr = new StringBuffer();
			for (int i = 0; i < tradeDTO.getFields().length; i++) {
				fieldStr.append(tradeDTO.getFields()[i]);
				if (i < tradeDTO.getFields().length - 1)
					fieldStr.append(",");
			}
			TaobaoClient client = new DefaultTaobaoClient(serverurl, appKey, appSecret);
			TradeFullinfoGetRequest req = new TradeFullinfoGetRequest();
			req.setFields(fieldStr.toString());
			req.setTid(tradeDTO.getTid());

			TradeFullinfoGetResponse rsp;
			String sessionKey = "";
			if (tradeDTO.getSeller_name().equals("西门子家电官方旗舰店")) {
				sessionKey = sessionKeygf;
			} else if (tradeDTO.getSeller_name().equals("西门子家电厨卫旗舰店")) {
				sessionKey = sessionKeycw;
			} else if (tradeDTO.getSeller_name().equals("博西华售后专营店")) {
				sessionKey = sessionKeysh;
			}

			rsp = client.execute(req, sessionKey);
			if (!rsp.isSuccess()) {
				JSONObject json = JSONObject.parseObject(rsp.getBody());
				String sub_code = JSONObject.parseObject(json.getString("error_response")).getString("sub_code");
				result.setError_code(sub_code);
				result.setSuccess(false);
				return result;
			}

			if (result == null) {
				result = new TradeFullResult();
			}
			String tradesjson = JSONObject.parseObject(rsp.getBody()).getString("trade_fullinfo_get_response");
			result = JSONObject.parseObject(tradesjson, TradeFullResult.class);
			result.setSuccess(true);
			System.out.println("trade_fullinfo_get_response :********" + rsp.getBody() + "******");
			System.out.println("响应结果:>>>>>" + JSONObject.toJSONString(result));

		} catch (ApiException e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/refunds/recevie")
	@ResponseBody
	public RefundsRecevieResult getRefundsRecevie(@RequestBody RefundsRecevieDTO refundsDTO) {
		RefundsRecevieResult result = new RefundsRecevieResult();
		try {
			StringBuffer fieldStr = new StringBuffer();
			for (int i = 0; i < refundsDTO.getFields().length; i++) {
				fieldStr.append(refundsDTO.getFields()[i]);
				if (i < refundsDTO.getFields().length - 1)
					fieldStr.append(",");
			}
			TaobaoClient client = new DefaultTaobaoClient(serverurl, appKey, appSecret);
			RefundsReceiveGetRequest req = new RefundsReceiveGetRequest();
			req.setFields(fieldStr.toString());
			if (refundsDTO.getStart_modified() != null) {
				req.setStartModified(com.taobao.api.internal.util.StringUtils.parseDateTime(refundsDTO.getStart_modified()));
			}
			if (refundsDTO.getEnd_modified() != null) {
				req.setEndModified(com.taobao.api.internal.util.StringUtils.parseDateTime(refundsDTO.getEnd_modified()));
			}

			if (refundsDTO.getPage_no() != null) {
				req.setPageNo(refundsDTO.getPage_no());
			}
			if (refundsDTO.getPage_size() != null) {
				req.setPageSize(refundsDTO.getPage_size());
			}
			if (refundsDTO.getUse_has_next() != null) {
				req.setUseHasNext(refundsDTO.getUse_has_next());
			}
			if (refundsDTO.getStatus() != null) {
				req.setStatus(refundsDTO.getStatus());
			}
			if (refundsDTO.getType() != null) {
				req.setType(refundsDTO.getType());
			}
			if (refundsDTO.getBuyer_nick() != null) {
				req.setBuyerNick(refundsDTO.getBuyer_nick());
			}

			RefundsReceiveGetResponse rsp;
			String sessionKey = "";
			if (refundsDTO.getSeller_name().equals("西门子家电官方旗舰店")) {
				sessionKey = sessionKeygf;
			} else if (refundsDTO.getSeller_name().equals("西门子家电厨卫旗舰店")) {
				sessionKey = sessionKeycw;
			} else if (refundsDTO.getSeller_name().equals("博西华售后专营店")) {
				sessionKey = sessionKeysh;
			}

			rsp = client.execute(req, sessionKey);
			if (!rsp.isSuccess()) {
				JSONObject json = JSONObject.parseObject(rsp.getBody());
				String sub_code = JSONObject.parseObject(json.getString("error_response")).getString("sub_code");
				result.setError_code(sub_code);
				result.setSuccess(false);
				return result;
			}

			if (result == null) {
				result = new RefundsRecevieResult();
			}
			String tradesjson = JSONObject.parseObject(rsp.getBody()).getString("refunds_receive_get_response");
			result = JSONObject.parseObject(tradesjson, RefundsRecevieResult.class);
			result.setSuccess(true);
			System.out.println("refunds_receive_get_response :********" + rsp.getBody() + "******");
			System.out.println("响应结果:>>>>>" + JSONObject.toJSONString(result));

		} catch (ApiException e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/refund/get")
	@ResponseBody
	public RefundResult getRefund(@RequestBody RefundDTO refundDTO) {
		RefundResult result = new RefundResult();
		try {
			StringBuffer fieldStr = new StringBuffer();
			for (int i = 0; i < refundDTO.getFields().length; i++) {
				fieldStr.append(refundDTO.getFields()[i]);
				if (i < refundDTO.getFields().length - 1)
					fieldStr.append(",");
			}
			TaobaoClient client = new DefaultTaobaoClient(serverurl, appKey, appSecret);
			RefundGetRequest req = new RefundGetRequest();
			req.setFields(fieldStr.toString());
			req.setRefundId(refundDTO.getRefund_id());

			RefundGetResponse rsp;
			String sessionKey = "";
			if (refundDTO.getSeller_name().equals("西门子家电官方旗舰店")) {
				sessionKey = sessionKeygf;
			} else if (refundDTO.getSeller_name().equals("西门子家电厨卫旗舰店")) {
				sessionKey = sessionKeycw;
			} else if (refundDTO.getSeller_name().equals("博西华售后专营店")) {
				sessionKey = sessionKeysh;
			}

			rsp = client.execute(req, sessionKey);
			if (!rsp.isSuccess()) {
				JSONObject json = JSONObject.parseObject(rsp.getBody());
				String sub_code = JSONObject.parseObject(json.getString("error_response")).getString("sub_code");
				result.setError_code(sub_code);
				result.setSuccess(false);
				return result;
			}

			if (result == null) {
				result = new RefundResult();
			}
			String tradesjson = JSONObject.parseObject(rsp.getBody()).getString("refund_get_response");
			result = JSONObject.parseObject(tradesjson, RefundResult.class);
			result.setSuccess(true);
			System.out.println("refund_get_response :********" + rsp.getBody() + "******");
			System.out.println("响应结果:>>>>>" + JSONObject.toJSONString(result));
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return result;
	}

}
